#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：init_en.py
@Author  ：赵加响
@Date    ：2025/5/7 10:48 
@Desc    ：使用init_en命令初始化英文版DB，这些DB为系统运行的基础，与init命令不可重复执行。
"""
from django.core.management import BaseCommand

from apps.accounts.models import SystemSettings, User
from apps.rbac.models import Operate, Menu, MenuOperate, Role
from utils.system_settings.constant import PASSWORD_VALID_DAYS, PASSWORD_CHANGE_REMINDER_DAYS, INITIAL_PASSWORD, \
    DISABLE_PASSWORD_DUPLICATION_TIMES, PASSWORD_REGEX


class Command(BaseCommand):
    help = 'Initialize DB Data'

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('Initialize System Settings DB.'))
        SystemSettings.objects.get_or_create(code=PASSWORD_VALID_DAYS,
                                             defaults={'value': '365', 'content': 'Password validity days',
                                                       'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=PASSWORD_CHANGE_REMINDER_DAYS,
                                             defaults={'value': '7', 'content': 'Password change reminder days',
                                                       'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=INITIAL_PASSWORD,
                                             defaults={'value': '123456', 'content': 'Default initial password.',
                                                       'value_type': 'str'})
        SystemSettings.objects.get_or_create(code=DISABLE_PASSWORD_DUPLICATION_TIMES,
                                             defaults={'value': '', 'content': 'Prohibited password repetition count',
                                                       'value_type': 'int'})
        SystemSettings.objects.get_or_create(code=PASSWORD_REGEX,
                                             defaults={
                                                 'value': '',
                                                 'content': 'Password rule regular expression', 'value_type': 'str'})

        self.stdout.write(self.style.SUCCESS('Initialize Operates DB.'))
        inquiry, created = Operate.objects.get_or_create(code='INQUIRY',
                                                         defaults={'name': 'Inquiry', 'name_en': 'Inquiry'})
        create, created = Operate.objects.get_or_create(code='CREATE', defaults={'name': 'Create', 'name_en': 'Create'})
        update, created = Operate.objects.get_or_create(code='UPDATE', defaults={'name': 'Update', 'name_en': 'Update'})
        delete, created = Operate.objects.get_or_create(code='DELETE', defaults={'name': 'Delete', 'name_en': 'Delete'})
        authorize, created = Operate.objects.get_or_create(code='AUTHORIZE',
                                                           defaults={'name': 'Authorize', 'name_en': 'Authorize'})
        assigning, created = Operate.objects.get_or_create(code='ASSIGNING',
                                                           defaults={'name': 'Assigning', 'name_en': 'Assigning'})
        reset, created = Operate.objects.get_or_create(code='RESET', defaults={'name': 'Reset', 'name_en': 'Reset'})
        release, created = Operate.objects.get_or_create(code='RELEASE',
                                                         defaults={'name': 'Release', 'name_en': 'Release'})
        close, created = Operate.objects.get_or_create(code='CLOSE', defaults={'name': 'Close', 'name_en': 'Close'})
        download, created = Operate.objects.get_or_create(code='DOWNLOAD',
                                                          defaults={'name': 'Download', 'name_en': 'Download'})
        upload, created = Operate.objects.get_or_create(code='UPLOAD',
                                                        defaults={'name': 'Upload', 'name_en': 'Upload'})

        self.stdout.write(self.style.SUCCESS('Initialize Menus DB.'))
        # 主页设定为失效状态
        Menu.objects.get_or_create(code='HOME-1000',
                                   defaults={'name': 'Home', 'name_en': 'Home', 'content': 'Home', 'is_active': False})
        rbac, created = Menu.objects.get_or_create(code='RBAC-1000',
                                                   defaults={'name': 'Permission', 'name_en': 'Permission',
                                                             'content': 'Permission'})
        account, created = Menu.objects.get_or_create(code='RBAC-1010',
                                                      defaults={'name': 'User Management', 'name_en': 'User Management',
                                                                'content': 'User Management', 'parent': rbac})

        role, created = Menu.objects.get_or_create(code='RBAC-1020',
                                                   defaults={'name': 'Role Management', 'name_en': 'Role Management',
                                                             'content': 'Role Management', 'parent': rbac})

        system, created = Menu.objects.get_or_create(code='SYSTEM-1000',
                                                     defaults={'name': 'System Setting', 'name_en': 'System Setting',
                                                               'content': 'System Setting'})
        parameter, created = Menu.objects.get_or_create(code='SYSTEM-1010',
                                                        defaults={'name': 'System Parameters',
                                                                  'name_en': 'System Parameters',
                                                                  'content': 'System Parameters', 'parent': system})
        logs, created = Menu.objects.get_or_create(code='SYSTEM-1020',
                                                   defaults={'name': 'Logs', 'name_en': 'Logs',
                                                             'content': 'Logs', 'parent': system})

        self.stdout.write(self.style.SUCCESS('Initialize MenuOperates DB.'))

        user_inquiry, created = MenuOperate.objects.get_or_create(menu=account, operate=inquiry)
        user_create, created = MenuOperate.objects.get_or_create(menu=account, operate=create)
        user_update, created = MenuOperate.objects.get_or_create(menu=account, operate=update)
        user_delete, created = MenuOperate.objects.get_or_create(menu=account, operate=delete)
        user_reset, created = MenuOperate.objects.get_or_create(menu=account, operate=reset)
        role_inquiry, created = MenuOperate.objects.get_or_create(menu=role, operate=inquiry)
        role_create, created = MenuOperate.objects.get_or_create(menu=role, operate=create)
        role_update, created = MenuOperate.objects.get_or_create(menu=role, operate=update)
        role_delete, created = MenuOperate.objects.get_or_create(menu=role, operate=delete)
        role_authorize, created = MenuOperate.objects.get_or_create(menu=role, operate=authorize)
        role_assigning, created = MenuOperate.objects.get_or_create(menu=role, operate=assigning)
        parameter_inquiry, created = MenuOperate.objects.get_or_create(menu=parameter, operate=inquiry)
        parameter_update, created = MenuOperate.objects.get_or_create(menu=parameter, operate=update)
        logs_inquiry, created = MenuOperate.objects.get_or_create(menu=logs, operate=inquiry)
        logs_delete, created = MenuOperate.objects.get_or_create(menu=logs, operate=delete)
        logs_download, created = MenuOperate.objects.get_or_create(menu=logs, operate=download)

        self.stdout.write(self.style.SUCCESS('Initialize MenuOperateUrls DB.'))
        user_inquiry.menu_operate_urls.get_or_create(description='User - Inquire', url='/account/list/', method='GET')
        user_create.menu_operate_urls.get_or_create(description='User - Create', url='/account/list/', method='POST')
        user_update.menu_operate_urls.get_or_create(description='User - Update', url='/account/list/', method='PUT')
        user_delete.menu_operate_urls.get_or_create(description='User - Delete', url='/account/list/', method='DELETE')
        user_reset.menu_operate_urls.get_or_create(description='User - Reset Password', url='/account/reset_password/',
                                                   method='POST')
        role_inquiry.menu_operate_urls.get_or_create(description='Role - Inquire', url='/rbac/role/', method='GET')
        role_create.menu_operate_urls.get_or_create(description='Role - Create', url='/rbac/role/', method='POST')
        role_update.menu_operate_urls.get_or_create(description='Role - Update', url='/rbac/role/', method='PUT')
        role_delete.menu_operate_urls.get_or_create(description='Role - Delete', url='/rbac/role/', method='DELETE')
        role_authorize.menu_operate_urls.get_or_create(description='Role - Menu Authorize',
                                                       url='/rbac/role_menu_authorize/', method='POST')
        role_assigning.menu_operate_urls.get_or_create(description='Role - Assign User', url='/rbac/role_assign_user/',
                                                       method='POST')
        parameter_inquiry.menu_operate_urls.get_or_create(description='Parameter - Inquire',
                                                          url='/system_settings/list/', method='GET')
        parameter_update.menu_operate_urls.get_or_create(description='Parameter - Update', url='/system_settings/list/',
                                                         method='PUT')
        logs_inquiry.menu_operate_urls.get_or_create(description='Log - Files',
                                                     url='/system_settings/logs_file_handles/', method='GET')
        logs_inquiry.menu_operate_urls.get_or_create(description='Log - Directory',
                                                     url='/system_settings/logs_directory_structure/', method='GET')
        logs_delete.menu_operate_urls.get_or_create(description='Log - Delete File',
                                                    url='/system_settings/delete_log_file/', method='POST')
        logs_delete.menu_operate_urls.get_or_create(description='Log - Delete Directory',
                                                    url='/system_settings/delete_log_folder/', method='POST')
        logs_download.menu_operate_urls.get_or_create(description='Log - Download',
                                                      url='/system_settings/download_log_file/', method='GET')

        self.stdout.write(self.style.SUCCESS('Initialize Roles DB.'))
        role, created = Role.objects.get_or_create(code='admin',
                                                   defaults={'name_zh': 'Administrator', 'name_en': 'Administrator',
                                                             'is_admin': True})
        if created:
            self.stdout.write(self.style.SUCCESS('Create role (code: admin, administrator: true)'))

        self.stdout.write(self.style.SUCCESS('Initialize Users DB.'))

        user, created = User.objects.get_or_create(username='admin',
                                                   defaults={'name': 'Administrator', 'is_superuser': True,
                                                             'is_staff': True, 'is_active': True, 'init_status': False,
                                                             'user_type': 'A', 'invalid_date': '2049-12-31'})

        if created:
            user.roles.set(Role.objects.filter(code__exact='admin'))
            user.set_password('123456')
            user.save()
            self.stdout.write(
                self.style.SUCCESS('Create user (username: admin, password: 123456, role: admin, superuser: true)'))
